<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--[if lt IE 9]><meta http-equiv="X-UA-Compatible" content="chrome=1" /><![endif]-->
    <!--[if IE 9]><meta http-equiv="X-UA-Compatible" content="IE9" /><![endif]-->
    <meta charset="UTF-8" />
    
    <title>jOWE ~ Examples ~ jowe-core.js (part 1)</title>
    
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="jowe-core.js"></script>
    <script type="text/javascript" src="jowe-alea.js"></script>

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20897630-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

</head>
<!--
********************************************************************************
jOWE - javascript Opensource Word Engine
http://code.google.com/p/jowe/
********************************************************************************

Copyright (c) 2010-2012 Ludovic L.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

********************************************************************************

How to use the HeightMap object.

-->
<body>
<em>This page is a part of the <a href="http://code.google.com/p/jowe/">jOWE</a> project - a javascript Opensource Word Engine</em>
<hr />

  <p>This page explains how to use the HeightMap object in the "jowe-core.js" file. You can give it a try at the bottom of the page.</p>
  
  <h1>Initialization</h1>

    <p>Calling all methods individually :</p>

<pre>
  <font color=green>// Create an object with default values :
  // pitch = 8, ratio = 3.1, width = 127, height = 127</font>
  var oMap = new HeightMap();
</pre>
  
  <h1>Method 1</h1>
  <p>Calling all methods individually :</p>

<pre>
  <font color=green>// Set the new size.</font>
  oMap.setSide(10, 12);
  
  <font color=green>// Process the random height map.</font>
  oMap.makeMap();
</pre>

  <p>At this stage, the array (oMap.item) contains :</p>
  
<pre>
  [[8,6,6,5,6,4,4,3,3,3,4,4,5,4,4,4,4] 
  ,[6,6,5,5,5,4,3,3,3,3,4,4,4,4,4,4,4] 
  ,[6,6,6,5,5,4,4,3,3,3,4,4,4,4,4,4,4] 
  ,[5,5,5,5,4,4,3,3,3,3,4,4,4,4,4,4,4] 
  ,[6,5,5,5,5,4,4,3,4,3,4,4,5,4,4,4,4] 
  ,[4,4,4,4,3,3,3,3,3,3,4,4,4,4,4,4,4] 
  ,[4,4,4,3,3,3,3,2,3,3,4,4,4,4,4,4,4] 
  ,[3,3,3,3,2,2,2,2,3,3,3,4,4,4,4,4,4] 
  ,[3,3,3,2,2,2,2,2,3,2,3,3,5,4,4,4,5] 
  ,[2,2,2,2,2,2,2,2,2,2,3,3,4,4,4,4,4] 
  ,[2,2,2,2,2,2,2,1,2,2,3,3,4,4,4,4,4] 
  ,[2,2,2,2,1,1,1,1,1,2,2,3,3,3,4,4,4] 
  ,[3,2,2,2,2,1,1,1,1,1,2,2,3,3,4,4,5] 
  ,[1,1,1,1,1,1,1,1,1,1,2,2,3,3,4,4,4] 
  ,[1,1,1,0,1,1,1,0,1,1,2,2,3,3,4,4,5] 
  ,[0,0,0,0,0,0,0,0,1,1,2,2,3,3,4,4,5] 
  ,[0,0,0,0,0,0,0,0,1,1,2,2,4,3,4,4,6]]
</pre>

<pre>
  <font color=green>// Optional, but smoothing the map is often required to avoid weird points and
  // produce better looking map. For now, smooth only worked on non-cropped map.
  // Once the map is cropped, smooth will failed (will be fixed in a next release).</font>
  oMap.smooth();  
</pre>

  <p>At this stage, the array (oMap.item) contains the values below. You should notice some small variations.</p>
  
<pre>
  [[8,6,6,5,6,4,4,3,3,3,4,4,5,4,4,4,4] 
  ,[6,6,6,5,5,4,4,3,3,3,4,4,4,4,4,4,4] 
  ,[6,6,5,5,5,4,4,3,3,3,4,4,4,4,4,4,4] 
  ,[5,5,5,5,5,4,4,3,3,3,4,4,4,4,4,4,4] 
  ,[6,5,5,5,4,4,3,3,3,3,4,4,4,4,4,4,4] 
  ,[4,4,4,4,4,3,3,3,3,3,4,4,4,4,4,4,4] 
  ,[4,4,4,3,3,3,3,3,3,3,4,4,4,4,4,4,4] 
  ,[3,3,3,3,2,2,2,3,3,3,3,4,4,4,4,4,4] 
  ,[3,3,3,2,2,2,2,2,2,3,3,4,4,4,4,4,5] 
  ,[2,2,2,2,2,2,2,2,2,2,3,3,4,4,4,4,4] 
  ,[2,2,2,2,2,2,2,2,2,2,3,3,3,4,4,4,4] 
  ,[2,2,2,2,2,2,1,1,1,2,2,3,3,3,4,4,4] 
  ,[3,2,2,2,2,1,1,1,1,1,2,2,3,3,4,4,5] 
  ,[1,1,1,1,1,1,1,1,1,1,2,2,3,3,4,4,4] 
  ,[1,1,1,1,1,1,1,1,1,1,2,2,3,3,4,4,5] 
  ,[0,0,0,0,0,0,0,1,1,1,2,2,3,3,4,4,5] 
  ,[0,0,0,0,0,0,0,0,1,1,2,2,4,3,4,4,6]]
</pre>

<pre>  <font color=green>// Finally, crop the working map to get the map with the requested size (10,12) as set previously.</font>
  oMap.crop();
</pre>

  <p>At the end, the array (oMap.item) contains :</p>
  
<pre>
  [[6,6,5,5,4,4,3,3,3,4,4,4] 
  ,[6,5,5,5,4,4,3,3,3,4,4,4] 
  ,[5,5,5,5,4,4,3,3,3,4,4,4] 
  ,[5,5,5,4,4,3,3,3,3,4,4,4] 
  ,[4,4,4,4,3,3,3,3,3,4,4,4] 
  ,[4,4,3,3,3,3,3,3,3,4,4,4] 
  ,[3,3,3,2,2,2,3,3,3,3,4,4] 
  ,[3,3,2,2,2,2,2,2,3,3,4,4] 
  ,[2,2,2,2,2,2,2,2,2,3,3,4] 
  ,[2,2,2,2,2,2,2,2,2,3,3,3]]
</pre>

  <h1>Method 2</h1>
  <p>Calling the "all-in-one" function :</p>

<pre><font color=green>  // Shortcut to generate a map (direct calls to "setSide", "makeMap", "smooth" and "crop").</font>
  oMap.doMap(10,12);
</pre>

  <p>You get the final result in only one call, the array (oMap.item) contains :</p>
  
<pre>
  [[6,6,5,5,4,4,3,3,3,4,4,4] 
  ,[6,5,5,5,4,4,3,3,3,4,4,4] 
  ,[5,5,5,5,4,4,3,3,3,4,4,4] 
  ,[5,5,5,4,4,3,3,3,3,4,4,4] 
  ,[4,4,4,4,3,3,3,3,3,4,4,4] 
  ,[4,4,3,3,3,3,3,3,3,4,4,4] 
  ,[3,3,3,2,2,2,3,3,3,3,4,4] 
  ,[3,3,2,2,2,2,2,2,3,3,4,4] 
  ,[2,2,2,2,2,2,2,2,2,3,3,4] 
  ,[2,2,2,2,2,2,2,2,2,3,3,3]]
</pre>

  <h1>Working with <em>pitch</em> and <em>ratio</em> parameters</h1>

  <p>
    The map is an array which contains values (integer) between 0 and {<b>pitch</b>}.<br />
    You can set an higher pitch, but it will more effective on bigger maps.</p>
<pre><font color=green>  // Create an HeightMap with a pitch of 99.</font>
  var oMap = new HeightMap(99);
  oMap.doMap(14, 34);

  [[90,88,86,84,83,81,79,77,75,74,72,71,70,68,67,66,65,64,64,63,63,63,62,61,61,60,59,58,58,58,57,57,57,57]
  ,[88,86,85,83,82,80,77,76,75,73,72,70,69,67,66,65,64,64,63,63,63,62,62,61,60,60,59,58,58,58,57,57,57,57]
  ,[86,85,84,83,81,79,77,75,74,73,72,70,68,67,65,64,64,63,63,63,62,62,62,61,60,60,59,58,58,58,57,57,57,57]
  ,[84,84,83,82,80,78,76,74,73,72,71,69,68,66,64,63,63,63,63,62,62,62,61,61,60,59,59,58,58,57,57,57,57,57]
  ,[83,82,81,80,79,77,75,73,72,71,70,68,67,65,64,63,62,62,62,62,62,62,61,60,60,59,58,58,57,57,57,57,57,57]
  ,[81,81,80,79,78,76,74,73,71,70,69,67,66,65,63,63,62,62,62,62,62,61,61,60,59,58,58,57,57,57,57,57,57,57]
  ,[80,80,79,78,77,76,74,72,71,69,68,67,66,64,63,62,62,62,62,62,61,61,61,60,59,58,57,57,57,56,56,56,57,57]
  ,[79,78,78,77,76,75,74,72,70,69,67,66,65,64,63,62,62,62,62,61,61,61,60,60,59,58,57,56,56,56,56,56,56,56]
  ,[77,77,76,75,75,74,73,71,70,68,67,66,65,64,63,62,62,62,61,61,61,60,60,59,58,57,56,56,55,55,55,55,55,56]
  ,[76,75,75,74,73,72,71,70,69,68,66,65,64,64,63,62,62,62,61,61,60,60,59,58,57,56,56,55,55,55,55,55,55,55]
  ,[74,74,74,73,72,71,70,69,68,67,66,65,64,64,63,63,62,62,61,61,60,59,58,58,57,56,55,55,54,54,54,54,54,54]
  ,[73,73,73,72,71,70,69,68,67,67,66,65,64,64,63,63,62,62,61,61,60,59,58,57,56,56,55,54,54,53,53,54,54,54]
  ,[72,72,72,71,70,69,68,67,67,66,65,64,64,64,63,63,62,61,61,60,59,58,57,56,56,55,54,54,53,53,53,53,53,53]
  ,[71,71,71,70,69,68,67,67,66,65,65,64,64,64,63,63,62,61,60,60,59,58,57,56,55,54,53,53,53,52,52,52,52,52]]
</pre>

  <p>
    The ratio parameter is used in the calculation of the height. By lowering it, you can
    have an higher difference between adjacent points.<br />
    Using a ratio of 0.54 for the previous example will give you the result below. Instead of having values between 90 and 52, you get
    values between 87 and 10.
  </p>

<pre><font color=green>  // Create an HeightMap with a pitch of 99 and a ratio of 0.54</font>
  var oMap = new HeightMap(99, 0.54);
  oMap.doMap(14, 34);

  [[87,83,79,75,71,68,66,63,58,53,47,42,35,30,24,20,18,18,17,17,16,14,12,11,11,12,12,11,10,10,11,12,13,14]
  ,[85,82,79,75,71,68,65,62,57,52,47,41,35,30,24,20,18,18,17,16,16,15,14,14,13,13,11,11,10,11,12,13,14,15]
  ,[83,81,79,75,71,67,64,61,57,53,48,42,36,30,25,21,19,18,17,17,16,16,15,15,15,14,13,11,11,12,12,13,14,15]
  ,[80,78,77,74,70,66,62,59,56,52,47,41,35,29,25,21,19,18,18,18,18,17,16,16,16,15,14,14,14,14,14,14,15,16]
  ,[76,74,73,71,68,64,60,57,54,49,44,38,33,29,25,22,20,19,19,19,19,18,17,17,17,17,17,17,17,17,17,17,17,18]
  ,[72,71,70,68,66,62,58,55,51,46,40,36,32,29,25,23,22,21,21,21,20,19,18,18,18,19,20,21,21,20,20,19,20,21]
  ,[70,69,68,66,64,61,57,53,49,43,39,35,31,28,26,24,24,23,23,22,21,20,19,19,20,21,23,24,24,23,23,23,23,22]
  ,[67,66,64,62,60,58,56,52,47,42,37,33,30,27,25,25,24,24,23,23,22,21,20,20,21,23,25,26,25,25,25,24,23,22]
  ,[63,61,59,57,56,55,53,50,46,41,36,32,28,26,25,24,24,23,22,22,21,20,20,21,22,24,26,27,26,26,25,24,23,22]
  ,[58,57,55,54,53,52,50,48,44,40,34,30,26,24,24,23,23,22,21,20,20,21,21,22,23,24,26,26,26,25,25,24,23,22]
  ,[54,53,52,51,50,49,47,45,42,37,32,27,24,23,22,22,22,21,20,19,20,21,22,23,24,25,25,25,26,25,25,24,23,22]
  ,[50,50,49,48,47,46,44,42,38,35,30,26,23,21,20,19,20,20,19,19,20,22,22,23,24,24,25,25,25,25,25,24,23,21]
  ,[47,47,46,46,45,44,42,38,35,32,29,25,22,20,17,16,17,18,18,19,20,21,22,22,23,24,24,24,24,25,25,24,23,22]
  ,[45,45,45,44,44,42,39,36,33,31,28,25,22,18,15,14,14,15,16,18,19,20,21,22,22,22,23,23,24,25,26,26,25,24]]
</pre>
  
  <h1>Try it</h1>

  <div id="tresult">
    
  </div>

  <p>
    Enter 'Pitch' and 'Ratio' :<br />
    <input id="tpitch" type="text" value="8" size="6" maxlength="2" style="text-align:right;">
      &
    <input id="tratio" type="text" value="3.1" size="6" maxlength="4" style="text-align:right;"><br />
    Enter 'width' x 'height' :<br />
    <input id="twidth" type="text" value="10" size="6" maxlength="3" style="text-align:right;">
      x
    <input id="theight" type="text" value="12" size="6" maxlength="3" style="text-align:right;">
    <input id="tsubmit" type="submit" value="Generate">
  </p>

<script type="text/javascript">

function run_test(p, r, w, h)
{
  var oMap;

  // Create object with default values :
  // pitch = 8, ratio = 3.1, width = 127, height = 127
  oMap = new HeightMap(p, r);
  
  // Uncomment line below only if you want to have same result as example.
  //oMap.setAleaSeed('jowe');
  
  var start = new Date().getTime();
  
  // Shortcut to generate a map (direct calls to "setSide", "makeMap", "smooth" and "crop").
  oMap.doMap(w, h);
  
  var end = new Date().getTime();

  $("#tresult").html("<pre>" +
                     "oMap.item = " + JSON.stringify(oMap.item).replace(/],/g,"]<br />            ,") + ";" +
                     "</pre>" +
                     "<p><em>Heightmap processed in " + (end - start) + "ms</em></p>");
}

$(function () {

  $("#tsubmit").click(function() {
    run_test( 1 * $("#tpitch").val(), 1.0 * $("#tratio").val(), 1 * $("#twidth").val(), 1 * $("#theight").val() );
  });

});
  
</script>

  <h2>What's next?</h2>

  <p>
    We put some <a href="examples-jowe-core-colored.html">color</a> on that map.
  </p>

<hr />
<em>This page is a part of the <a href="http://code.google.com/p/jowe/">jOWE</a> project - a javascript Opensource Word Engine</em>
</body>
</html>